﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace hlavny.modul2
{
    [Serializable]
    public class CDatabase : IComparable
    {
        private int id;
        private string name;
        private List<int> tablesIds = null;     //list of database tables ids
        [NonSerialized]
        private List<CTable> tablesRefs = null; //help list of references to database tables objects 

        #region CDatabase properties

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public List<int> TablesIds
        {
            get { return tablesIds; }
            set { tablesIds = value; }
        }
        public List<CTable> TablesRefs
        {
            get { return tablesRefs; }
            set { tablesRefs = value; }
        }

        #endregion

        #region CDatabase constructor overloads

        /// <summary>
        /// CDatabase constructor
        /// </summary>
        /// <param name="id">id of the new database</param>
        /// <param name="name">name of the new database</param>
        public CDatabase(int id, string name)
        {
            this.id = id;
            this.name = name;
            this.tablesIds = new List<int>();
        }

        public CDatabase(int id)
        {
            this.id = id;
        }

        #endregion

        #region CDatabase main functions

        /// <summary>
        /// Function reload all references to database tables
        /// </summary>
        /// <param name="sysTables">CSysTables object</param>
        public void ReloadTables(CSysTables sysTables)
        {
            this.tablesRefs = new List<CTable>();
            foreach (int id in this.tablesIds)
            {
                int index = sysTables.GetTableIndex(id);
                if (index >= 0)
                {
                    tablesRefs.Add(sysTables.Tables[index]);
                }
            }
            tablesRefs.Sort();
        }

        /// <summary>
        /// Function find out if the database already containts table with entered name
        /// </summary>
        /// <param name="name">name ov the table</param>
        /// <param name="sysTables">CSysTables object</param>
        /// <returns>true if database containts table, otherwise false</returns>
        public bool ContaintTableName(string name, CSysTables sysTables)
        {
            if (tablesIds.Count > 0)
            {
                foreach (int id in tablesIds)
                {
                    if (sysTables.Tables[sysTables.GetTableIndex(id)].Name == name)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        #endregion

        #region IComparable Members

        public int CompareTo(object obj)
        {
            CDatabase tmp = obj as CDatabase;
            return this.id.CompareTo(tmp.Id);
        }

        #endregion

        #region ToString function override

        public override string ToString()
        {
            return this.name;
        }
        
        #endregion
    }
}
